package net.jwtools.tmt.ui;

import java.text.NumberFormat;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import net.jwtools.tmt.service.IVisitService;
import net.jwtools.tmt.service.VisitService;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SummaryPieJPanel extends JPanel {
	private IVisitService visitService;
	
	public SummaryPieJPanel(IVisitService service) {
		setVisitService(service);
	}

	public JComponent getControl() {
		JFreeChart chart = createChart(createDataset());
		ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(269, 241));
		return chartPanel;
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		ApplicationContext context = new ClassPathXmlApplicationContext(
		        new String[] {"/net/jwtools/tmt/ctx/tmt-application-context.xml", "/net/jwtools/tmt/ctx/tmt-data-access-context.xml"});

		BeanFactory factory = (BeanFactory) context;
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		SummaryPieJPanel spp = new SummaryPieJPanel((VisitService)factory.getBean("visitService"));
		frame.setContentPane(spp.getControl());
		frame.pack();
		frame.setVisible(true);

	}

	public IVisitService getVisitService() {
		return visitService;
	}

	public void setVisitService(IVisitService visitService) {
		this.visitService = visitService;
	}

	private PieDataset createDataset() {

		DefaultPieDataset pieDataset = new DefaultPieDataset();
		pieDataset.setValue("Checked out (Not Home)", visitService
				.countAllOutstandingReVisits());
		pieDataset.setValue("Checked out (Regular)", visitService
				.countAllOutstandingMainVisits());
		pieDataset.setValue("Checked in (Regular)", visitService
				.countAllCompletedMainVisits());
		pieDataset.setValue("Checked in (Not Home)", visitService
				.countAllCompletedReVisits());
		return pieDataset;
	}

	private JFreeChart createChart(PieDataset dataset) {
		JFreeChart summaryChart = ChartFactory.createPieChart3D(
				"Summary checked in / checked out", dataset, true, true, false);
		// summaryChart.setBackgroundPaint(Color.white);
		PiePlot3D plot = (PiePlot3D) summaryChart.getPlot();
		plot.setForegroundAlpha(0.6f);
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0} = {1} ({2})", NumberFormat.getNumberInstance(),
				NumberFormat.getPercentInstance()));
		return summaryChart;

	}

}
